package com.ynet.middleground.user.service.impl;

import cfca.ra.toolkit.exception.RATKException;
import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.*;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.common.SystemConstant;
import com.ynet.middleground.user.dao.CertificateLogMapper;
import com.ynet.middleground.user.dto.*;
import com.ynet.middleground.user.entity.CertificateLog;
import com.ynet.middleground.user.model.CertificateManageModel;
import com.ynet.middleground.user.service.CertificateManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 证书管理 服务实现类
 * </p>
 *
 * @author Wangxz
 * @since 2019-11-05
 */
@Path("certificateManage")
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
@Api(value = "证书管理", tags = {"证书管理接口"})
@Service(timeout = 6000, version = "0.0.1")
public class CertificateManageServiceImpl implements CertificateManageService, GenericService {

    @Autowired
    CertificateManageModel certificateManageModel;
    
    @Autowired
    CertificateLogMapper certificateLogMapper;
    
    @Autowired
    Mapper mapper;

    @POST
    @Path("generate-cert")
    @ApiOperation(value = "CFCA证书申请并下载服务", notes = "CFCA证书申请并下载服务")
    @Override
    public ServiceResult<GenerateCertDto> generateCert(GenerateCertReq generateCertReq) {
        ServiceResult<GenerateCertDto> generateCertDtoServiceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(generateCertReq, CertificateLog.class);
        certificateLog.setCreateBy(generateCertReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_CREATE);
        certificateLog.setRequestMsg(generateCertReq.toString());
        try {
            generateCertDtoServiceResult.setResult(certificateManageModel.generateCert(generateCertReq));
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            certificateLog.setResponseMsg(generateCertDtoServiceResult.getResult().toString());
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            generateCertDtoServiceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(be.getCode()+be.getMessage());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            generateCertDtoServiceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setResponseMsg(rae.getCode()+rae.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            // 捕获系统异常
        } catch (Exception e) {
            generateCertDtoServiceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行证书生成异常,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(Throwables.getStackTraceAsString(e));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return generateCertDtoServiceResult;
    }

    @POST
    @Path("update-and-download-cert")
    @ApiOperation(value = "CFCA证书更新并下载服务", notes = "CFCA证书更新并下载服务")
    @Override
    public ServiceResult<UpdateDownloadCertDto> updateAndDownloadCert(UpdateDownloadCertReq updateDownloadCertReq) {
        ServiceResult<UpdateDownloadCertDto> updateDownloadCertDtoServiceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(updateDownloadCertReq, CertificateLog.class);
        certificateLog.setCreateBy(updateDownloadCertReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_UPDATE_DOWNLOAD);
        certificateLog.setRequestMsg(updateDownloadCertReq.toString());
        try {
            updateDownloadCertDtoServiceResult.setResult(certificateManageModel.updateAndDownloadCert(updateDownloadCertReq));
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            certificateLog.setResponseMsg(updateDownloadCertDtoServiceResult.getResult().toString());
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            updateDownloadCertDtoServiceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(be.getCode()+be.getMessage());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            updateDownloadCertDtoServiceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(updateDownloadCertDtoServiceResult.getErrorCode()+updateDownloadCertDtoServiceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            updateDownloadCertDtoServiceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书更新并下载服务,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(updateDownloadCertDtoServiceResult.getErrorCode()+updateDownloadCertDtoServiceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return updateDownloadCertDtoServiceResult;
    }

    @POST
    @Path("freeze-cert")
    @ApiOperation(value = "CFCA证书冻结服务", notes = "CFCA证书冻结服务")
    @Override
    public ServiceResult<String> freezeCert(CertFreezeReq certFreezeReq) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certFreezeReq, CertificateLog.class);
        certificateLog.setCreateBy(certFreezeReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_FREEZE);
        certificateLog.setRequestMsg(certFreezeReq.toString());
        try {
            certificateManageModel.freezeCert(certFreezeReq);
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            serviceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            serviceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            serviceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书冻结服务,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return serviceResult;
    }

    @POST
    @Path("thaw-cert")
    @ApiOperation(value = "CFCA证书解冻服务", notes = "CFCA证书解冻服务")
    @Override
    public ServiceResult<String> thawCert(CertThawReq certThawReq) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certThawReq, CertificateLog.class);
        certificateLog.setCreateBy(certThawReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_UNFREEZE);
        certificateLog.setRequestMsg(certThawReq.toString());
        try {
            certificateManageModel.thawCert(certThawReq);
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            serviceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            serviceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            serviceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书解冻服务,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return serviceResult;
    }

    @POST
    @Path("revoke-cert")
    @ApiOperation(value = "CFCA证书吊销服务", notes = "CFCA证书吊销服务")
    @Override
    public ServiceResult<String> revokeCert(CertRevokeReq certRevokeReq) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certRevokeReq, CertificateLog.class);
        certificateLog.setCreateBy(certRevokeReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_CANCEL);
        certificateLog.setRequestMsg(certRevokeReq.toString());
        try {
            certificateManageModel.revokeCert(certRevokeReq);
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            serviceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            serviceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            serviceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书吊销服务,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(serviceResult.getErrorCode()+serviceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return serviceResult;
    }

    @POST
    @Path("update-cert")
    @ApiOperation(value = "CFCA证书更新服务", notes = "CFCA证书更新服务")
    @Override
    public ServiceResult<CertUpdateDto> updateCert(CertUpdateReq certUpdateReq) {
        ServiceResult<CertUpdateDto> certUpdateDtoServiceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certUpdateReq, CertificateLog.class);
        certificateLog.setCreateBy(certUpdateReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_UPDATE);
        certificateLog.setRequestMsg(certUpdateReq.toString());
        try {
            certUpdateDtoServiceResult.setResult(certificateManageModel.updateCert(certUpdateReq));
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            certificateLog.setResponseMsg(certUpdateDtoServiceResult.getResult().toString());
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            certUpdateDtoServiceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certUpdateDtoServiceResult.getErrorCode()+certUpdateDtoServiceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            certUpdateDtoServiceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certUpdateDtoServiceResult.getErrorCode()+certUpdateDtoServiceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            certUpdateDtoServiceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书更新服务异常,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(certUpdateDtoServiceResult.getErrorCode()+certUpdateDtoServiceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return certUpdateDtoServiceResult;
    }

    @POST
    @Path("download-cert")
    @ApiOperation(value = "CFCA证书制证服务", notes = "CFCA证书制证服务")
    @Override
    public ServiceResult<CertDownloadDto> downloadCert(CertDownloadReq certDownloadReq) {
        ServiceResult<CertDownloadDto> certDownloadDtoServiceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certDownloadReq, CertificateLog.class);
        certificateLog.setCreateBy(certDownloadReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_DOWNLOAD);
        certificateLog.setRequestMsg(certDownloadReq.toString());
        try {
            certDownloadDtoServiceResult.setResult(certificateManageModel.downloadCert(certDownloadReq));
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            certificateLog.setResponseMsg(certDownloadDtoServiceResult.getResult().toString());
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            certDownloadDtoServiceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certDownloadDtoServiceResult.getErrorCode()+certDownloadDtoServiceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            certDownloadDtoServiceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certDownloadDtoServiceResult.getErrorCode()+certDownloadDtoServiceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            certDownloadDtoServiceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书下载服务服务异常,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(certDownloadDtoServiceResult.getErrorCode()+certDownloadDtoServiceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return certDownloadDtoServiceResult;
    }

    @POST
    @Path("reissue-cert")
    @ApiOperation(value = "CFCA证书补发服务", notes = "CFCA证书补发服务")
    @Override
    public ServiceResult<CertReissueDto> reissueCert(CertReissueReq certReissueReq) {
        ServiceResult<CertReissueDto> certReissueDtoServiceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certReissueReq, CertificateLog.class);
        certificateLog.setCreateBy(certReissueReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_REISSUE);
        certificateLog.setRequestMsg(certReissueReq.toString());
        try {
            certReissueDtoServiceResult.setResult(certificateManageModel.reissueCert(certReissueReq));
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            certificateLog.setResponseMsg(certReissueDtoServiceResult.getResult().toString());
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            certReissueDtoServiceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certReissueDtoServiceResult.getErrorCode()+certReissueDtoServiceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            certReissueDtoServiceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certReissueDtoServiceResult.getErrorCode()+certReissueDtoServiceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            certReissueDtoServiceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书补发服务异常,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(certReissueDtoServiceResult.getErrorCode()+certReissueDtoServiceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return certReissueDtoServiceResult;
    }

    @POST
    @Path("renewal-cert")
    @ApiOperation(value = "CFCA证书换发服务", notes = "CFCA证书换发服务")
    @Override
    public ServiceResult<CertRenewalDto> renewalCert(CertRenewalReq certRenewalReq) {
        ServiceResult<CertRenewalDto> certRenewalDtoServiceResult= new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certRenewalReq, CertificateLog.class);
        certificateLog.setCreateBy(certRenewalReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_RENEWAL);
        certificateLog.setRequestMsg(certRenewalReq.toString());
        try {
            certRenewalDtoServiceResult.setResult(certificateManageModel.renewalCert(certRenewalReq));
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            certificateLog.setResponseMsg(certRenewalDtoServiceResult.getResult().toString());
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            certRenewalDtoServiceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certRenewalDtoServiceResult.getErrorCode()+certRenewalDtoServiceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            certRenewalDtoServiceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(certRenewalDtoServiceResult.getErrorCode()+certRenewalDtoServiceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            certRenewalDtoServiceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书换发服务异常,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(certRenewalDtoServiceResult.getErrorCode()+certRenewalDtoServiceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return certRenewalDtoServiceResult;
    }

    @POST
    @Path("query-cert-info")
    @ApiOperation(value = "CFCA证书查询服务", notes = "CFCA证书查询服务")
    @Override
    public ServiceResult<List<CertQueryDto>> queryCertInfo(CertQueryReq certQueryReq) {
        ServiceResult<List<CertQueryDto>> listServiceResult = new ServiceResult<>();
        CertificateLog certificateLog = mapper.map(certQueryReq, CertificateLog.class);
        certificateLog.setCreateBy(certQueryReq.getOperationUserId());
        certificateLog.setGmtCreate(LocalDateTime.now());
        certificateLog.setOperationType(SystemConstant.CFCA_OPERATION_QUERY);
        certificateLog.setRequestMsg(certQueryReq.toString());
        try {
            listServiceResult.setResult(certificateManageModel.queryCertInfo(certQueryReq));
            certificateLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            certificateLog.setResponseMsg(listServiceResult.getResult().toString());
            // 捕获业务逻辑异常
        } catch (BusinessException be) {
            IfpLogger.error("generateCert", "业务逻辑异常,case:{}", Throwables.getStackTraceAsString(be));
            listServiceResult.setError(be.getCode(), be.getMessage());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(listServiceResult.getErrorCode()+listServiceResult.getErrorMsg());
            // 捕获CFCA证书服务异常
        } catch (RATKException rae) {
            listServiceResult.setError("00001", "CFCA服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "CFCA服务异常,case:{}", Throwables.getStackTraceAsString(rae));
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            certificateLog.setResponseMsg(listServiceResult.getErrorCode()+listServiceResult.getErrorMsg());
            // 捕获系统异常
        } catch (Exception e) {
            listServiceResult.setError("00001", "系统服务异常，请联系系统管理员。");
            IfpLogger.error("generateCert", "执行CFCA证书查询服务异常,case:{}", Throwables.getStackTraceAsString(e));
            certificateLog.setResponseMsg(listServiceResult.getErrorCode()+listServiceResult.getErrorMsg());
            certificateLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        }
        certificateLogMapper.insert(certificateLog);
        return listServiceResult;
    }
    
    /* cfca证书绑定begin */
    @POST
    @Path("binding-cert")
    @ApiOperation(value = "用户绑定cfca证书信息", notes = "将组织id与ukey编号及该ukey编号对应的证书信息绑定")
    @Override
    public ServiceResult<String> bindingCert(BindingCertReq requestObject) {
        ServiceResult<String> resultService = new ServiceResult<>();
        String result = null;
        try {
            result = certificateManageModel.bindingCert(requestObject);
        } catch (BusinessException be) {
            resultService.setError(be.getCode(), be.getMessage());
            return resultService;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            resultService.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return resultService;
        }
        return resultService.setResult(result);
    }
}
